import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
import VCalendar from 'v-calendar';
import 'v-calendar/style.css';
import Cookies from 'js-cookie'
import VueHashCalendar from 'vue3-hash-calendar';
import 'vue3-hash-calendar/es/index.css';

import App from './App.vue'
import router from './router'
import 'font-awesome/css/font-awesome.min.css';
import Common from './components/Common.vue'
import Footer from './components/Footer.vue'
import {user,pinia} from "@/stores/counter.js";

const app = createApp(App)

app.use(pinia)
app.use(router)
app.use(ElementPlus)
app.use(VueHashCalendar);
app.use(VCalendar, {})
app.component('common', Common)
app.component('footer', Footer)


router.beforeEach((to, from, next) => {
	const token = Cookies.get("doctor_token");  // 获取token

	if(to.path === '/'||
		to.path === '/index'||
		to.path === '/register'||to.path === '/login') {  // 在为空的情况下，路由跳转的是'/' 就放行。'/'代表登录页
		next()
	} else {           // 在为空的情况下，访问的不是'/'， 就直接跳转到'/'
		if(token) {
			// 如果token不为null 就放行，如果为空 就不放行
			const userInfo=user()
			if(userInfo.userId===''){
				// ElMessage.warning('正在拉取用户消息');
				userInfo.getInfo().then(()=> {
					next();
					ElMessage.success('用户消息拉取成功')
				}).catch(()=>{
					ElMessage.error('用户消息拉取失败，请先登录')
					next({
						path:'/login'
					})
				});
			}else {
				next();
			}
		}else {
			next({
				path:'/login'
			})
		}
	}

})

app.mount('#app')
